using System;
using System.Diagnostics;
using System.ServiceModel;
using OpenComposite.HumanActivity.Contracts;


namespace OpenComposite.HumanActivity.Data
{
	internal static class Global
	{
		static Global()
		{
			Global.DataAccess = new DataAccess();
			Global.EventLog = null;
		}

		internal static DataAccess DataAccess { get; private set; }

		internal static EventLog EventLog { get; set; }

		internal static FaultException<GenericFault> CreateAndLogGenericFault(
			string faultSource, Exception exception)
		{
			FaultException<GenericFault> fault = new FaultException<GenericFault>(
				new GenericFault(faultSource, exception.ToString()), exception.Message);
			WriteToEventLog(fault.ToString(), EventLogEntryType.Error);
			return fault;
		}
		internal static FaultException<HumanActivityFault> CreateAndLogHumanActivityFault(
			string faultSource, Exception ex)
		{
			return CreateAndLogHumanActivityFault(faultSource, ex.Message, ex.ToString(), -1, -1);
		}
		internal static FaultException<HumanActivityFault> CreateAndLogHumanActivityFault(
			string faultSource, Exception ex, int activityId, int userId)
		{
			return CreateAndLogHumanActivityFault(faultSource, ex.Message, ex.ToString(), activityId, userId);
		}
		internal static FaultException<HumanActivityFault> CreateAndLogHumanActivityFault(
			string faultSource, string message, string details)
		{
			return CreateAndLogHumanActivityFault(faultSource, message, details, -1, -1);
		}
		internal static FaultException<HumanActivityFault> CreateAndLogHumanActivityFault(
			string faultSource, string message, string details, int activityId, int userId)
		{
			HumanActivityFault fault = new HumanActivityFault(faultSource, activityId, userId, details);
			WriteToEventLog(fault.ToString(), EventLogEntryType.Error);
			return new FaultException<HumanActivityFault>(fault, message);
		}

		internal static void WriteToEventLog(string message, EventLogEntryType type)
		{
			if ( Global.EventLog != null ) {
				Global.EventLog.WriteEntry(message, type);
			} else {
				Trace.WriteLine(message, type.ToString());
			}
		}

	}
}
